import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/health/title_view.dart';
import 'package:flutter_eve_plugin/components/main/refresh_view.dart';
import 'package:flutter_eve_plugin/model/health/health_log.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/store_vm/health/main.dart';
import 'package:flutter_eve_plugin/utils/image_util.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class WeightAnalysisWidget extends StatefulWidget {
  final String title;
  final HealthLogModel logModel;
  final String type;
  final void Function(
          String type, int lastTime, GlobalKey<RefreshHeaderState> headerKey)
      logRefresh;
  final void Function(
          String type, int lastTime, GlobalKey<RefreshFooterState> footerKey)
      logLoadMore;

  const WeightAnalysisWidget(
      {Key key,
      this.title,
      this.logModel,
      this.type,
      this.logRefresh,
      this.logLoadMore})
      : super(key: key);

  @override
  _WeightAnalysisWidgetState createState() => _WeightAnalysisWidgetState();
}

class _WeightAnalysisWidgetState extends State<WeightAnalysisWidget> {
  GlobalKey<RefreshHeaderState> _headerKey = GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey = GlobalKey<RefreshFooterState>();

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
        converter: (store) => AppStateViewModel(
            state: store.state,
            healthStateViewModel:
                HealthStateViewModel(logRefresh: (String type, int lastTime) {
              widget.logRefresh(type, lastTime, _headerKey);
            }, logLoadMore: (String type, int lastTime) {
              widget.logLoadMore(type, lastTime, _footerKey);
            })),
        builder: (context, vm) {
          if (widget.logModel != null &&
              widget.logModel.enabled &&
              widget.logModel.list.isNotEmpty) {
            return Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(imageUtilHuPo('health/icon_dotted_border@2x')),
                    fit: BoxFit.fill),
              ),
              width: setRatio(345),
              height: setRatio(300),
              margin: EdgeInsets.only(
                  left: setRatio(15),
                  right: setRatio(15),
                  bottom: setRatio(30)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SizedBox(
                    height: setRatio(25),
                  ),
                  HealthTitleViewWidget(
                    title: widget.title,
                  ),
                  SizedBox(
                    height: setRatio(25),
                  ),
                  Container(
                      width: setRatio(345),
                      height: setRatio(195),
                      child: EasyRefresh(
                          refreshHeader: buildClassicsHeader(_headerKey),
                          refreshFooter: buildClassicsFooter(_footerKey),
                          onRefresh: () async {
                            vm.healthStateViewModel.logRefresh(
                              widget.type,
                              0,
                            );
                          },
                          loadMore: () async {
                            vm.healthStateViewModel.logLoadMore(widget.type,
                                widget.logModel?.list?.last?.clearingTime);
                          },
                          child: ListView.builder(
                              physics: BouncingScrollPhysics(),
                              itemCount: widget.logModel.list.length,
                              itemBuilder: (BuildContext context, int index) {
                                return Container(
                                  color: index % 2 == 0
                                      ? Color.fromRGBO(121, 117, 255, 0.1)
                                      : Colors.transparent,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Expanded(
                                        child: Container(
                                          child: Text(
                                            DateTime.fromMillisecondsSinceEpoch(
                                                    widget.logModel.list[index]
                                                        .clearingTime)
                                                .toString()
                                                .substring(5, 16),
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    153, 153, 153, 1),
                                                fontSize: setTextSp(11),
                                                fontWeight: FontWeight.w500),
                                          ),
                                          padding: EdgeInsets.only(
                                              left: setRatio(14),
                                              top: setRatio(10),
                                              bottom: setRatio(10)),
                                        ),
                                        flex: 1,
                                      ),
                                      Expanded(
                                        child: Container(
                                          child: Text(
                                            widget.logModel.list[index].desc,
                                            maxLines: 2,
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                              color: Color.fromRGBO(
                                                  153, 153, 153, 1),
                                              fontSize: setTextSp(11),
                                            ),
                                          ),
                                          padding: EdgeInsets.only(
                                              right: setRatio(29),
                                              top: setRatio(10),
                                              bottom: setRatio(10)),
                                        ),
                                        flex: 2,
                                      ),
                                    ],
                                  ),
                                );
                              })))
                ],
              ),
            );
          } else {
            return Container();
          }
        });
  }
}
